<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>书城首页</title>
    <%@include file="/pages/common/head.jsp" %>


    <script type="text/javascript">
        $(function () {

            $("#btn").click(function () {
                var val = $("#pn_input").val();
                if (1 <= val && val <=${requestScope.page.pageTotal}) {
                    return true;
                } else if (val >${requestScope.page.pageTotal}) {
                    alert("输入页号大于最大页号，自动跳转到末页！");
                } else if (val < 1) {
                    alert("输入页号小于最小页号，自动跳转到首页！");
                }
            })

            $("#search").click(function () {
                var val1 = $("#min").val();
                var val2 = $("#max").val();
                if (val1 == "" || val2 == "" || val1 > val2 || val1 < 0 || val2 < 0) {
                    alert("请输入正确的价格范围！");
                    return false;
                }
            })

            $("#out").click(function () {
                return confirm("确定要注销吗？");
            })

            $(".cart").click(function () {
                if (${sessionScope.user==null}) {
                    alert("请先登录！");
                } else {
                    alert("加入成功！");
                }
            })
        })
    </script>
</head>
<body>

<div id="header">
    <img class="logo_img" alt="" src="static/img/logo.gif">
    <span class="wel_word">网上书城</span>
    <div>
        <c:if test="${empty sessionScope.username}">
            <a href="pages/user/login.jsp">登录</a>
            <a href="pages/user/regist.jsp">注册</a> &nbsp;&nbsp;
            <a href="pages/cart/cart.jsp">购物车</a>
            <a href="pages/manager/manager.jsp">后台管理</a>
        </c:if>
        <c:if test="${not empty sessionScope.username}">
            <span>欢迎<span class="um_span">${sessionScope.username}</span>光临尚硅谷书城</span>
            <a href="orderServlet?action=showMyOrders&id=${sessionScope.user.id}">我的订单</a>
            <a href="pages/cart/cart.jsp">购物车</a>
            <a href="pages/manager/manager.jsp">后台管理</a>
            <a id="out" href="userServlet?action=logout">注销</a>&nbsp;&nbsp;
        </c:if>

    </div>
</div>
<div id="main">

    <div id="book">
        <div class="book_cond">
            <form action="client/bookServlet" method="get">
                <input type="hidden" name="action" value="pagingByPrice">
                价格：<input id="min" type="text" name="min" value="${requestScope.min}"> 元 -
                <input id="max" type="text" name="max" value="${requestScope.max}"> 元
                <input id="search" type="submit" value="查询"/>
            </form>
        </div>

        <div style="text-align: center">
            <c:if test="${empty sessionScope.cart.items}">
                <span>您的购物车中有0件商品</span>
            </c:if>
            <c:if test="${not empty sessionScope.cart.items}">
                <span>您的购物车中有${sessionScope.cart.totalcount}件商品</span>
                <div>
                    您刚刚将<span style="color: red">${sessionScope.lastName}</span>加入到了购物车中
                </div>
            </c:if>
        </div>

        <c:forEach items="${requestScope.page.items}" var="book">
            <div class="b_list">
                <div class="img_div">
                    <img class="book_img" alt="" src="static/img/default.jpg"/>
                </div>
                <div class="book_info">
                    <div class="book_name">
                        <span class="sp1">书名:</span>
                        <span class="sp2">${book.name}</span>
                    </div>
                    <div class="book_author">
                        <span class="sp1">作者:</span>
                        <span class="sp2">${book.author}</span>
                    </div>
                    <div class="book_price">
                        <span class="sp1">价格:</span>
                        <span class="sp2">￥${book.price}</span>
                    </div>
                    <div class="book_sales">
                        <span class="sp1">销量:</span>
                        <span class="sp2">${book.sales}</span>
                    </div>
                    <div class="book_amount">
                        <span class="sp1">库存:</span>
                        <span class="sp2">${book.stock}</span>
                    </div>
                    <div class="book_add">
                        <form action="cartServlet" method="post">
                            <input type="hidden" name="action" value="addItem">
                            <input type="hidden" name="id" value="${book.id}">
                            <c:if test="${book.stock>0}">
                                <input type="submit" class="cart" value="加入购物车">
                            </c:if>
                            <c:if test="${book.stock==0}">
                                <label>售完</label>
                            </c:if>
                        </form>
                    </div>
                </div>
            </div>
        </c:forEach>

    </div>
    <%--处理分页--%>
    <div id="page_nav">

        <a href="client/bookServlet?action=pagingByPrice&pageNum=${1}&min=${requestScope.min}&max=${requestScope.max}">首页</a>

        <c:if test="${requestScope.page.pageNum>1}">
            <a href="client/bookServlet?action=pagingByPrice&pageNum=${requestScope.page.pageNum-1}&min=${requestScope.min}&max=${requestScope.max}">上一页</a>
        </c:if>

        <c:choose>
            <c:when test="${requestScope.page.pageTotal<=5}">
                <c:forEach begin="1" end="${requestScope.page.pageTotal}" var="i">
                    <c:if test="${i!=requestScope.page.pageNum}">
                        <a href="client/bookServlet?action=pagingByPrice&pageNum=${i}&min=${requestScope.min}&max=${requestScope.max}"> ${i}</a>
                    </c:if>
                    <c:if test="${i==requestScope.page.pageNum}">
                        <<${i}>>
                    </c:if>
                </c:forEach>
            </c:when>

            <c:when test="${requestScope.page.pageTotal>5}">
                <c:choose>
                    <c:when test="${requestScope.page.pageNum<=3}">
                        <c:forEach begin="1" end="5" var="i">
                            <c:if test="${i!=requestScope.page.pageNum}">
                                <a href="client/bookServlet?action=pagingByPrice&pageNum=${i}&min=${requestScope.min}&max=${requestScope.max}">${i}</a>
                            </c:if>
                            <c:if test="${i==requestScope.page.pageNum}">
                                <<${i}>>
                            </c:if>
                        </c:forEach>
                    </c:when>
                    <c:when test="${requestScope.page.pageNum>requestScope.page.pageTotal-3}">
                        <c:forEach begin="${requestScope.page.pageTotal-4}" end="${requestScope.page.pageTotal}"
                                   var="i">
                            <c:if test="${i!=requestScope.page.pageNum}">
                                <a href="client/bookServlet?action=pagingByPrice&pageNum=${i}&min=${requestScope.min}&max=${requestScope.max}">${i}</a>
                            </c:if>
                            <c:if test="${i==requestScope.page.pageNum}">
                                <<${i}>>
                            </c:if>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <c:forEach begin="${requestScope.page.pageNum-2}" end="${requestScope.page.pageNum+2}"
                                   var="i">
                            <c:if test="${i!=requestScope.page.pageNum}">
                                <a href="client/bookServlet?action=pagingByPrice&pageNum=${i}&min=${requestScope.min}&max=${requestScope.max}">${i}</a>
                            </c:if>
                            <c:if test="${i==requestScope.page.pageNum}">
                                <<${i}>>
                            </c:if>
                        </c:forEach>
                    </c:otherwise>
                </c:choose>
            </c:when>
        </c:choose>

        <c:if test="${requestScope.page.pageNum<requestScope.page.pageTotal}">
            <a href="client/bookServlet?action=pagingByPrice&pageNum=${requestScope.page.pageNum+1}&min=${requestScope.min}&max=${requestScope.max}">下一页</a>
        </c:if>
        <a href="client/bookServlet?action=pagingByPrice&pageNum=${requestScope.page.pageTotal}&min=${requestScope.min}&max=${requestScope.max}">末页</a>

        共${requestScope.page.pageTotal}页，${requestScope.page.pageTotalCount}条记录
        <br/>
        <form method="post" action="client/bookServlet">
            到第
            <input type="hidden" name="action" value="forpagingByPrice">
            <input type="hidden" name="max" value="${requestScope.max}">
            <input type="hidden" name="min" value="${requestScope.min}">
            <input value="${requestScope.pn}" name="pn" id="pn_input"/>页
            <input id="btn" type="submit" value="确定">
        </form>
    </div>


</div>

<%@include file="/pages/common/foot.jsp" %>
</body>
</html>